package com.ulook.lookplanet.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.WidgetData;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.ui.square.topic.TopicActivity;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

/**
 * 描述：热点话题组件
 *
 * @author L1998
 * @data 2021/4/14
 */
public class HotTopicView extends FrameLayout implements WidgetData<TTopic> {

    private TTopic topic;
    private Context context;
    private int currUserId;

    private QMUIRadiusImageView2 imageView;
    private TextView textView;

    public HotTopicView(Context context) {
        super(context);
        initHotTopicView(context);
    }

    public HotTopicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initHotTopicView(context);
    }

    public HotTopicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initHotTopicView(context);
    }

    private void initHotTopicView(Context context){
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.widget_hot_topic, this, true);
        imageView = findViewById(R.id.widget_hot_topic_image);
        imageView.setImageResource(R.mipmap.topic);
        textView = findViewById(R.id.widget_hot_topic_text);
    }


    public TTopic getTopic(){
        return topic;
    }

    @Override
    public void setWidgetData(TTopic value) {
        this.topic = value;
        if(!(topic.getTopicimg() == null || "".equals(topic.getTopicimg()))){
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    byte[] image = (byte[]) res;
                    //使用BitmapFactory工厂，把字节数组转化为bitmap
                    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                    imageView.setImageBitmap(bitmap);
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    ThrowException.print(failuer, failuer.getEmsg());
                }
            };
            AsynRequestUtil.loadImg(AppServer.DEFAULT_IMG_PATH + topic.getTopicimg(), null, callback);
        }
        textView.setText(topic.getName());
        textView.setMaxLines(1);

        // 话题详情
        this.setClickable(true);
        this.setOnClickListener(view -> {
            Intent intent = new Intent(context, TopicActivity.class);
            intent.putExtra("currUserId", currUserId);
            intent.putExtra("topic", topic);
            context.startActivity(intent);
        });
    }

    @Override
    public void setWidgetData(TTopic value, int currUserID) {
        this.currUserId = currUserID;
        setWidgetData(value);
    }
}
